-------------------------------------------------------------------------------------
-- -*- coding: utf-8 -*-
--
-- @author:   zxf
-- @email:    07291@qq.com
-- @date:     2024/5/29
-- @module:   ride
-- @describe: 坐骑
-- @version:  v1.0
--

-------------------------------------------------------------------------------------

---@class ride
local ride = {}
local this = ride
local trace = trace
---@type api
local api = import("api/api")
---@type common
local common = import("game/func/common")
---@type creature_cfg
local creature_cfg = import("game/config/creature_cfg")
local RIDER = creature_cfg.RIDER
local WEAPON_SHAPE = creature_cfg.WEAPON_SHAPE
local FLY = creature_cfg.FLY
local creature_unit = creature_unit
local creature_ctx = creature_ctx

-- 是否骑乘 0/坐骑 1/滑翔翼 2/武器外型
function this.is_use_ride(checkType)
    local ride_list = this.get_creature_list(checkType)
    -- 如果坐骑列表为空
    if table.is_empty(ride_list) then
        trace.log_trace("没有可以佩戴的坐骑")
        return false
    end
    for k, v in pairs(ride_list) do
        if v.is_used then
            return true
        end
    end
    return false
end

-- 穿戴骑乘
function this.use_ride_by_name(name, setType)
    if not name then
        trace.log_trace("没有名字")
        return false
    end
    if not setType then
        trace.log_trace("没有设置类型")
        return false
    end
    -- 获取坐骑的列表
    local ride_list = this.get_creature_list(setType)
    -- 如果坐骑列表为空
    if table.is_empty(ride_list) then
        trace.log_trace("没有可以佩戴的物品")
        return false
    end
    local res = nil
    for k, v in pairs(ride_list) do
        if v.name == name then
            res = v
            break
        end
    end
    if res then
        if not res.is_used then
            creature_unit.use_creature(res.id)
        end
        return true
    else
        return false
    end
end

-- 使用产物
function this.use_creature(obj)
    if creature_ctx:init(obj) then
        if not creature_ctx:is_used() then
            creature_unit.use_creature(creature_ctx:id())
        end
        return true
    end
    return false
end

-- 获取产物列表 0/坐骑 1/滑翔翼 2/武器外型
function this.get_creature_list(setType)
    local function index_of(t, val, k)
        for i = 1, #t do
            if t[i].name == val then
                return i
            end
        end
        return 999 + k
    end
    local selectRes
    if setType == 0 then
        selectRes = RIDER
    elseif setType == 1 then
        selectRes = FLY
    elseif setType == 2 then
        selectRes = WEAPON_SHAPE
    end

    local list = creature_unit.list(setType)
    local ret = {}
    local tmpData = nil
    for i = 1, #list do
        if creature_ctx:init(list[i]) then
            tmpData = {
                obj = list[i],
                id = creature_ctx:id(),
                type = creature_ctx:type(),
                num = creature_ctx:num(),
                quality = creature_ctx:quality(),
                is_used = creature_ctx:is_used(),
                name = creature_ctx:name(),
            }
            table.insert(ret, tmpData)
        end
    end
    local revCreatureList = common.reverseTable(ret)
    for k, v in ipairs(revCreatureList) do
        local idx = index_of(selectRes, v.name, k)
        v.idx = idx
    end
    return revCreatureList
end

-- 获取最佳物品
function this.get_best_creature(setType)
    local creatureList = this.get_creature_list(setType)
    if table.is_empty(creatureList) then
        return nil
    end
    table.sort(creatureList, function(a, b)
        if a.quality == b.quality then
            return a.idx < b.idx
        else
            return a.quality > b.quality
        end
    end)
    local ret = nil
    ret = creatureList[1]
    --for k, v in ipairs(creatureList) do
    --    if not ret then
    --        ret = v
    --    else
    --        if v.quality > ret.quality then
    --            ret = v
    --        end
    --    end
    --end

    if ret then
        if ret.is_used then
            return nil
        end
    end
    return ret
end

function ride.get_best_weapon_by_name(name)
    local creatureList = this.get_creature_list(2)
    if table.is_empty(creatureList) then
        return nil
    end
    table.sort(creatureList, function(a, b)
        if a.quality == b.quality then
            return a.idx < b.idx
        else
            return a.quality > b.quality
        end
    end)
    local findInfo
    for k, v in ipairs(creatureList) do
        if string.find(v.name, name) then
            if not findInfo then
                findInfo = v
            else
                if v.quality > findInfo.quality then
                    findInfo = v
                end
            end
        end
    end
    if findInfo then
        if findInfo.is_used then
            return nil
        end
    end
    return findInfo
end

--------------------------------------------------------------------------------
-- [读取] 根据品质返回拥有的坐骑/武器外形
---@param quality number 品质,白色品质1/绿色品质2/蓝色品质3
---@param nType number 坐骑0/滑翔翼1/武器外形2
---@return number   对应品质的数量
--------------------------------------------------------------------------------
function ride.get_creature_num_by_quality(nType, quality)
    local num = 0

    local list = api.get_creature_info(nType)
    for _, creature_info in ipairs(list) do
        if quality == creature_info.quality then
            num = num + creature_info.num
        end
    end
    return num
end

function ride.get_best_creature_info_by_res(nType)
    local bestInfo = {}
    local list = ride.get_creature_list(nType)
    if not table.is_empty(list) then
        if true then

        end
        table.sort(list, function(a, b)
            if a.idx and b.idx then
                return a.idx < b.idx
            end
        end)
        bestInfo = list[1]
    end

    return bestInfo
end

return this
